package com.nip.workmanager.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nip.workmanagement.R;
import com.nip.workmanager.entity.IconEntity;

public class IconAdapter extends BaseAdapter {

	private ArrayList<IconEntity> _arraySource;
	private Activity _activity;
	private static LayoutInflater _inflater;
	
	
	public IconAdapter(ArrayList<IconEntity> _arraySource, Activity _activity) {
		super();
		this._arraySource = _arraySource;
		this._activity = _activity;
		_inflater = (LayoutInflater) this._activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(_arraySource.size() <= 0)
		{
			return 1;
		}
		return _arraySource.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHoder viewHoder;
		View row = arg1;
		if(arg1 == null)
		{
			row = _inflater.inflate(R.layout.listview_icon_layout, null);
			viewHoder = new ViewHoder();
			viewHoder.tvIconName = (TextView) row.findViewById(R.id.tvIcoName);
			viewHoder.imIcon = (ImageView) row.findViewById(R.id.imIcon);
			
			row.setTag(viewHoder);
		}else
		{
			viewHoder = (ViewHoder) row.getTag();
		}
		
		if(_arraySource.size() <= 0)
		{
			viewHoder.tvIconName.setText("No data");
		}else
		{
			viewHoder.tvIconName.setText(_arraySource.get(arg0).getIconName());
			viewHoder.imIcon.setImageResource(_arraySource.get(arg0).getIcon());
		}
		return row;
	}

	/**
	 * Contain the control of layout 
	 * @author ThangNM
	 *
	 */
	public static class ViewHoder{
		
		public TextView tvIconName;
		public ImageView imIcon;
	}
	

}
